<?php

namespace App\Repositories;

use App\Models\Freight;
use App\Models\FreightArea;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class FreightRepository
 * @package App\Repositories
 * @version May 8, 2018, 5:16 pm CST
 *
 * @method Freight findWithoutFail($id, $columns = ['*'])
 * @method Freight find($id, $columns = ['*'])
 * @method Freight first($columns = ['*'])
*/
class FreightRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'shop_id',
        'name',
        'type',
        'status'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Freight::class;
    }

    public function checkArea($goods,$attributes,$address)
    {
        foreach ($goods as $v) {
            $freight = Freight::find($v->freight_id);
            //寻找 地址 $address
            $freight_area_ids = DB::table('freight_area_province')->where('province_id', $address->province_id)->pluck('freight_area_id')->toArray();
            $area_data = $freight->areas()->whereIn('id', $freight_area_ids)->first();
            if(!$area_data){
                return $v->name.'不在配送范围内';
            }
        }
        foreach ($attributes as $attribute) {
            $freight = Freight::find($attribute->goods->freight_id);
            //寻找 地址 $address
            $freight_area_ids = DB::table('freight_area_province')->where('province_id', $address->province_id)->pluck('freight_area_id')->toArray();
            $area_data = $freight->areas()->whereIn('id', $freight_area_ids)->first();
            if(!$area_data){
                return $attribute->goods->name.'不在配送范围内';
            }
        }
        return null;
    }

    public function calculate($goods,$attributes,$address)
    {
        $freights = [];
        foreach ($goods as $good) {
            if(isset($freights[$good->freight_id])){
                $freights[$good->freight_id][] = clone $good;
            }else{
                $freights[$good->freight_id][] = clone $good;
            }
        }
        foreach ($attributes as $attribute) {
            if(isset($freights[$attribute->goods->freight_id])){
                $freights[$attribute->goods->freight_id][] = clone $attribute;
            }else{
                $freights[$attribute->goods->freight_id][] = clone $attribute;
            }
        }
        $all_price = 0;
        foreach ($freights as $id=>$freight_array) {
            $all_count = 0;
            $all_weight = 0;
            $price = 0;
            $freight = Freight::find($id);
            //寻找 地址 $address
            $freight_area_ids = DB::table('freight_area_province')->where('province_id', $address->province_id)->pluck('freight_area_id')->toArray();
            $area_data = $freight->areas()->whereIn('id', $freight_area_ids)->first();
            foreach ($freight_array as $goods) {
                if($freight->type==1){
                    //按件
                    $all_count += $goods->_number;
                }else{
                    //按重量
                    $all_weight += $goods->weight;
                }
            }

            if($freight->type==1){
                //按件
                if($all_count <= $area_data->first_number){
                    $price += $area_data->first_price;
                }else{
                    $add_count = ceil(($all_count - $area_data->first_number)/$area_data->continue_number);
                    $price += $area_data->first_price + $add_count * $area_data->continue_price;
                }
            }else{
                //按重量
                if($all_weight <= $area_data->first_number){
                    $price += $area_data->first_price;
                }else{
                    $add_count = ceil(($all_weight - $area_data->first_number)/$area_data->continue_number);
                    $price += $area_data->first_price + $add_count * $area_data->continue_price;
                }
            }
            $all_price += $price;
        }
        return $all_price;
    }
}
